<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/GPL-3.0
 * @link https://www.gougucms.com
 */

namespace app\bitjob\model;

use think\facade\Db;
use think\Model;

class Bitjob extends Model
{
    const ZERO = 0;
    const ONE = 1;
    const TWO = 2;
    const THREE = 3;
    const FORE = 4;
    const FIVE = 5;
    const SIX = 6;
    public static $Status = [
        self::ZERO => '待审核',
        self::ONE => '审核中',
        self::TWO => '审核通过',
        self::THREE => '审核拒绝',
        self::FORE => '已撤销',
        self::FIVE => '已中止',
        self::SIX => '已作废',
    ];

    public static $PType = [
        '一级注册建筑师',
        '二级注册建筑师',
        '一级注册结构工程师',
        '二级注册结构工程师',
        '注册土木工程师(岩土)',
        '注册土木工程师(港口与航道工程)',
        '注册公用设备工程师(暖通空调)',
        '注册公用设备工程师(给水排水)',
        '注册公用设备工程师(动力)',
        '注册电气工程师(发输变电)',
        '注册电气工程师(供配电)',
        '注册化工工程师',
        '监理工程师',
        '一级注册建造师',
        '二级注册建造师',
        '造价工程师',
        '二级造价师',
        '三类人员A类',
        '三类人员B类',
        '三类人员C类',
        '施工员',
        '质量员',
        '安全员',
        '标准员',
        '材料员',
        '机械员',
        '劳务员',
        '资料员',
        '注册土木工程师（水利水电）',
        '注册土木工程师（道路工程）',
        '注册环保工程师',
    ];

    public static $CertType = [
        '房建',
        '市政',
        '机电',
        '水利',
        '公路',
        '矿业',
        '通信',
        '岩土',
        '港口与航道工程',
        '暖通空调',
        '给水排水',
        '动力',
        '发输变电',
        '供配电',
        '建筑',
        '造价（土建）',
        '造价（安装）',
        '水利水电工程',
        '注册化工',
        '注册环保',
        '注册结构',
        '道路工程',
    ];

    //列表检索
    public function get_list($param = [], $where = [], $whereOr = [])
    {

        $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
        $db = self::where($where)
            ->where(function ($query) use ($whereOr) {
                $query->whereOr($whereOr);
            })->field('a.*')
            ->alias('a');
        if (!empty($param['cert_valid_from'])) {
            if($param['cert_valid_from']==1){
                $db->where('a.cert_valid_date < DATE_ADD(CURDATE(), INTERVAL 6 MONTH)');
            }else{
                $db->where('a.cert_valid_date > DATE_ADD(CURDATE(), INTERVAL 6 MONTH)');
            }
        }
        $list = $db->order('a.create_time desc')
            ->paginate($rows, false, ['query' => $param])
            ->each(function ($item, $key) {
                $item->status_name = self::$Status[(int)$item->check_status];
                $item->type_name = self::$PType[$item->p_type-1];
                $item->cert_major_type = self::$CertType[$item->cert_major_type-1];
                $item->cert_valid_type = '';
                if($item->cert_valid_date=='0000-00-00'){
                    $item->cert_valid_date='';
                }
                if($item->cert_valid_date){
                    $times =   strtotime($item->cert_valid_date) - time();
                    if($times < 180*24*60*60){
                        $item->cert_valid_type = '即将到期';
                    }
                }
                if($item->abc_valid_date=='0000-00-00'){
                    $item->abc_valid_date='';
                }
                $item->abc_valid_type = '';
                if($item->abc_valid_date){
                    $times =   strtotime($item->abc_valid_date) - time();
                    if($times < 180*24*60*60){
                        $item->abc_valid_type = '即将到期';
                    }
                }
                $enterprise =  Db::name('bitjob_enterprise')->where("FIND_IN_SET('{$item->id}',bitjob_ids)")->where('reg_valid_date >= CURDATE()')->find();
                $item->cert_state = 0;
                $item->cert_money = '';
                $item->out_money = '';
                if(!empty($enterprise)){
                    if($enterprise['certs']){
                        $certs = json_decode($enterprise['certs'],true);
                        foreach ($certs as $k => $v){
                            if($v['bitjob_id'] == $item->id){
                                $item->cert_money = $v['new_money'] -   $item->money;
                                $item->out_money = $v['new_money'];
                            }
                        }
                    }
                    $item->cert_state = 1;
                }

            });
        return $list;
    }


    // 获取合同详情
    public function detail($id)
    {
        $detail = self::where(['id' => $id])->find();
        if (!empty($detail)) {
            $share_names = Db::name('Admin')->where([['id', 'in', $detail['share_ids']]])->column('name');
            $detail['share_names'] = implode(',', $share_names);
            if ($detail['file_ids'] != '') {
                $fileArray = Db::name('File')->where('id', 'in', $detail['file_ids'])->select();
                $detail['fileArray'] = $fileArray;
//                $detail['license_cert'] =  Db::name('license_cert')->where('license_id', $detail['id'])->where('is_delete', 0)->select();
            }
        }
        return $detail;
    }
}
